/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__bashead_parse.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2017/03/24 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <cq_city_payment/cq_city_payment_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frsutl.h"
#include "frreport_comm.h"
#include "frbyte_buffer.h"
#include "../../../src/module/cq_city_payment/config.h"

typedef struct __st_cq_city_payment_data
{
    L4_transaction st_l4_trans;                 //L4信息统计
    Frlist_header *p_report_comm;               //用于临时存储report的公共部分数据
    Frlist_header *p_report_req;                //用于临时存储report的请求部分数据
} cq_city_payment_data;

typedef struct __st_L7session_cq_city_payment_private
{
    unsigned long ul_seq;                       //序列号，1次req和response时，使用同一个序列号
    L4_transaction st_l4_trans;                 //数据
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frbyte_Packet *p_req_packet;                //存请求bytebuff数据
    Frbyte_Packet *p_resp_packet;               //存相应bytebuff数据
} L7session_cq_city_payment_private;

extern int __bashead_parse(Frbyte_Packet *p_packet
                        , Frreport_body_data *p_st_body_data
                        , char ***ppp_data_out
                        , int i_req_flag);
extern int cq_city_payment_session_destroy(void *p_private_session);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char **pp_mesgid = NULL;

    i_ret = __bashead_parse(p_packet, p_st_body_data, &pp_mesgid, 1);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

//第十位
static void case2(void)
{
    int i_ret = 0;
    char *p_data = 0;
    Frbyte_Packet *p_packet = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_mesgid = NULL;
    char **pp_mesgid = &p_mesgid;

    p_data = (char *)"\x7b\x31\x3a\x30\x30\x30\x30\x32\x37\x33\x00\x37\x35\x32\x20\x20\x20\x20\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x30\x30\x31\x36\x35\x33\x30\x39\x39\x39\x39\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x4d\x73\x67\x31\x37\x30\x33\x31\x37\x31\x36\x35\x34\x32\x35\x30\x30\x36\x30\x36\x32\x30\x31\x37\x30\x33\x31\x38\x32\x30\x31\x37\x30\x33\x31\x37\x31\x36\x35\x30\x33\x38\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x7b\x32\x3a\x3a\x53\x42\x4e\x3a\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x3a\x30\x35\x31\x3a\x32\x30\x31\x37\x30\x33\x31\x38\x3a\x4f\x43\x4e\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x31\x33\x36\x36\x31\x33\x32\x38\x30\x7d\x7b\x43\x3a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x45\x4e\x44";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 248) != FR_SUCC)
        goto ERROR;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    i_ret = __bashead_parse(p_packet, p_st_body_data, &pp_mesgid, 1);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
    
    bytebuff_free(&p_packet);
    frreport_body_free(&p_st_body_data);
    frfree(p_mesgid);
    return;

ERROR:
    bytebuff_free(&p_packet);
    frreport_body_free(&p_st_body_data);

    return;
}

static void case3(void)
{
    int i_ret = 0;
    char *p_data = 0;
    Frbyte_Packet *p_packet = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    char *p_mesgid = NULL;
    char **pp_mesgid = &p_mesgid;

    p_data = (char *)"\x7b\x31\x3a\x30\x30\x30\x30\x32\x37\x33\x42\x37\x35\x32\x20\x20\x20\x20\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x30\x30\x31\x36\x35\x33\x30\x39\x39\x39\x39\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x4d\x73\x67\x31\x37\x30\x33\x31\x37\x31\x36\x35\x34\x32\x35\x30\x30\x36\x30\x36\x32\x30\x31\x37\x30\x33\x31\x38\x32\x30\x31\x37\x30\x33\x31\x37\x31\x36\x35\x30\x33\x38\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x7b\x32\x3a\x3a\x53\x42\x4e\x3a\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x3a\x30\x35\x31\x3a\x32\x30\x31\x37\x30\x33\x31\x38\x3a\x4f\x43\x4e\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x31\x33\x36\x36\x31\x33\x32\x38\x30\x7d\x7b\x43\x3a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x45\x4e\x44";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 248) != FR_SUCC)
        goto ERROR;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    i_ret = __bashead_parse(p_packet, p_st_body_data, &pp_mesgid, 1);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    
    bytebuff_free(&p_packet);
    frreport_body_free(&p_st_body_data);
    frfree(p_mesgid);
    return;

ERROR:
    bytebuff_free(&p_packet);
    frreport_body_free(&p_st_body_data);

    return;
}

int add_suite__bashead_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __bashead_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
